#include <Arduino.h>
#include "signal_sender.h"

const int tick = 20;

SignalSender::SignalSender(int sig_pin){
    this->sig_pin = sig_pin;
    pinMode(sig_pin, OUTPUT);
}

void SignalSender::low_n(int n){
    digitalWrite(sig_pin, LOW);
    delay(tick * n);
}

void SignalSender::hign_n(int n){
    digitalWrite(sig_pin, HIGH);
    delay(tick * n);
}

void SignalSender::data1(){
    hign_n(2);
    low_n(2);
}

void SignalSender::data0(){
    hign_n(3);
    low_n(1);
}
  
void SignalSender::sig_begin(){
    hign_n(8);
    low_n(4);
}

void SignalSender::sig_end(){
    hign_n(4);
    low_n(8);
    Serial.println("");
}

void SignalSender::send_sig(char c){
    sig_begin();
    for(int i=0; i<8; i++){
        if(c & 1<<i){
            data1();
        }else{
            data0();
        }
    }
    sig_end();
}
